package com.dnjn.happiness.system.api.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.dnjn.happiness.common.core.annotation.Excel;
import com.dnjn.happiness.common.core.annotation.Excels;
import com.dnjn.happiness.common.core.web.domain.BaseEntity;
import com.dnjn.happiness.common.core.xss.Xss;
import com.dnjn.happiness.system.api.domain.SysDept;
import com.dnjn.happiness.system.api.domain.SysRole;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户对象 sys_user
 *
 * @author happy
 */

@Getter
@Setter
public class SysUser extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    @Excel(name = "用户序号", cellType = Excel.ColumnType.NUMERIC, prompt = "用户编号")
    private Long userId;

    /**
     * 部门ID
     */
    @Excel(name = "部门编号", type = Excel.Type.IMPORT)
    @NotNull(message = "部门编号不能为空")
    private Long deptId;

    /**
     * 岗位名称
     */
    @Excel(name = "岗位描述", type = Excel.Type.IMPORT)
    private String postName;

    @Excel(name = "工号")
    @NotBlank(message = "工号不能为空")
    private String empno;

    @Schema(description = "SAP流水号")
    @Excel(name = "SAP流水号")
    private String sapNo;


    @Schema(description = "职级")
    @Excel(name = "职级")
    @NotBlank(message = "职级不能为空")
    private String positionLadder;

    @Schema(description = "职等")
    @Excel(name = "职等")
    @NotNull(message = "职等不能为空")
    private Integer positionLevel;

    @Schema(description = "入职日期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "入职日期", width = 30, dateFormat = "yyyy-MM-dd", prompt = "yyyy-MM-dd")
    @NotNull(message = "入职日期不能为空")
    private LocalDate empDate;


    /**
     * 用户账号
     */
    @Xss(message = "用户账号不能包含脚本字符")
    @NotBlank(message = "用户账号不能为空")
    @Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符")
    @Excel(name = "登录名称")
    private String userName;

    /**
     * 用户昵称
     */
    @Xss(message = "用户昵称不能包含脚本字符")
    @NotBlank(message = "用户姓名不能为空")
    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
    @Excel(name = "姓名")
    private String nickName;

    /**
     * 用户邮箱
     */

    @Email(message = "邮箱格式不正确")
    @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
    @Excel(name = "用户邮箱")
    private String email;

    /**
     * 手机号码
     */

    @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
    @Excel(name = "手机号码")
    private String phonenumber;

    /**
     * 用户性别
     */
    @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
    @NotBlank(message = "用户性别不能为空")
    private String sex;

    @Excel(name = "用户生日")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate birthday;
    /**
     * 用户头像
     */
    private String avatar;

    /**
     * 个人履历地址
     */
    private String resumeurl;


    /**
     * 密码
     */

//    @JSONField(serialize = false)
    private String password;

    /**
     * 帐号状态（0正常 1停用）
     */
    @Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
    private String status;

    /**
     * 删除标志（0代表存在 2代表删除）
     */
    private String delFlag;

    /**
     * 最后登录IP
     */
    @Excel(name = "最后登录IP", type = Excel.Type.EXPORT)
    private String loginIp;

    /**
     * 最后登录时间
     */
    @Excel(name = "最后登录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Excel.Type.EXPORT)
    private LocalDateTime loginDate;

    /**
     * 部门对象
     */
    @Excels({
            @Excel(name = "部门名称", targetAttr = "deptName", type = Excel.Type.EXPORT)
    })
    private SysDept dept;

    /**
     * 当前用户父级部门信息
     */
    @TableField(exist = false)
    private SysDept leaderDept;

    /**
     * 角色对象
     */
    private List<SysRole> roles;

    /**
     * 角色组
     */
    private Long[] roleIds;

    /**
     * 岗位组
     */
    private Long[] postIds;

    /**
     * 岗位组
     */
    @TableField(exist = false)
    private String postNames;

    /**
     * 岗位 排序集合
     */
    @TableField(exist = false)
    private List<Integer> sortList;


    /**
     * 角色ID
     */
    private Long roleId;

    /**
     * 微信open_id
     */
    private String openId;

    /**
     * 微信union_id
     */
    private String unionId;

    /**
     * 婚姻 未婚、已婚、离异或丧偶。
     */
    //@NotBlank(message = "婚姻情况不能为空")
    @Schema(description = "UNMARRIED=未婚,MARRIED=已婚,DIVORCED=离婚,WIDOWED=丧偶")
    @Excel(name = "婚姻情况", readConverterExp = "UNMARRIED=未婚,MARRIED=已婚,DIVORCED=离婚,WIDOWED=丧偶")
    private String marriageCondition;


    /**
     * 儿女情况
     */
//    @Excel(name = "儿女情况")
    private String childrenCondition;

    /**
     * 儿女情况-女孩 children_condition_girl
     */
    @Excel(name = "儿女情况-女孩数量")
    private Integer childrenConditionGirl;

    /**
     * 儿女情况-男孩 children_condition_boy
     */
    @Excel(name = "儿女情况-男孩数量")
    private Integer childrenConditionBoy;

    /**
     * 用户专属头像地址
     */
    private String exclusiveAvatar;

    /**
     * 专属头像审核状态 0-审核中;1-审核通过;2-审核拒绝
     */
    private String exclusiveAvatarStatus;

    /**
     * 审核结果
     */
    private String processResult;

    /**
     * 被沟通人的快乐值
     */
    @TableField(exist = false)
    private Integer happinessScore;

    /**
     * 快乐豆
     */
    @TableField(exist = false)
    private Integer hlbean;

    /**
     * 近几日被沟通过
     */
    @TableField(exist = false)
    private Integer communicateCount;

    @TableField(exist = false)
    private String deptName;

    

    /**
     * 所属团队名称
     */
    @TableField(exist = false)
    private String parentTeamName;

    public SysUser() {

    }

    public SysUser(Long userId) {
        this.userId = userId;
    }

    public boolean isAdmin() {
        return isAdmin(this.userId);
    }

    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    } //todo

    public boolean isAdmin(List<Long> roleList) {
        return isAdmin(this.userId, roleList);
    }

    public static boolean isAdmin(Long userId, List<Long> roleList) {
        Boolean flag = false;
        if (null != roleList && !roleList.isEmpty()) {
            for (Long roleId : roleList) {
                if (1L == roleId) {
                    flag = true;
                }
            }
        }
        return userId != null && 1L == userId || flag;
    }


    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("userId", getUserId())
                .append("deptId", getDeptId())
                .append("userName", getUserName())
                .append("nickName", getNickName())
                .append("email", getEmail())
                .append("phonenumber", getPhonenumber())
                .append("sex", getSex())
                .append("avatar", getAvatar())
                .append("password", getPassword())
                .append("status", getStatus())
                .append("delFlag", getDelFlag())
                .append("loginIp", getLoginIp())
                .append("loginDate", getLoginDate())
                .append("createBy", getCreateBy())
                .append("createTime", getCreateTime())
                .append("updateBy", getUpdateBy())
                .append("updateTime", getUpdateTime())
                .append("remark", getRemark())
                .append("dept", getDept())
                .append("openId", getOpenId())
                .append("unionId", getUnionId())
                .toString();
    }
}
